<script lang="ts" setup>
import { onMounted } from 'vue';
import { parse as parseUserAgent } from '@/common/utils/user-agent';

const props = defineProps({
  from: {
    type: String,
    default: '/',
  },
});

onMounted(() => {
  // 检查 from 合法性，只支持站内地址
  if (!props.from.startsWith('/')) {
    window.location.href = '/';

    return;
  }

  const userAgent = parseUserAgent(navigator.userAgent);
  const base = `${window.location.protocol}//${window.location.host}`;
  const redirect = `${base}${import.meta.env.MICRO_SITE_BASE_URL}/callback?from=${encodeURIComponent(props.from)}`;

  // 拼接登录地址
  let loginPath = '';

  if (userAgent.browser.name === 'WeChat') {
    loginPath = `${import.meta.env.VITE_ATOMGIT_URL}/callback?transfer_redirect=${encodeURIComponent(redirect)}&transfer_type=wechat`;
  }
  else {
    loginPath = `${import.meta.env.VITE_ATOMGIT_URL}/callback?transfer_redirect=${encodeURIComponent(redirect)}`;
  }

  window.location.href = loginPath;
});
</script>

<template>
  <main>
    正在登录，请稍候...
  </main>
</template>
